package cn.yangyumanage.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.fragment.app.Fragment;

import com.google.android.flexbox.FlexboxLayout;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.List;

import cn.yangyumanage.R;
import cn.yangyumanage.application.DataApplication;
import cn.yangyumanage.bean.ImageQuality;
import cn.yangyumanage.bean.LargeClassification;
import cn.yangyumanage.bean.SmallClassification;
import cn.yangyumanage.bean.TheRegion;
import cn.yangyumanage.bean.TheYear;
import cn.yangyumanage.callback.DataCallBack;
import cn.yangyumanage.http.HttpClient;
import cn.yangyumanage.model.TagManageHttpModel;
import cn.yangyumanage.util.ToastUtil;
import cn.yangyumanage.util.Utils;

public class TagManageFragment extends Fragment {
    private final Context context;
    private final TagManageHttpModel httpModel = new TagManageHttpModel();
    private View view;
    private FlexboxLayout fl_large;
    private FlexboxLayout fl_small;
    private FlexboxLayout fl_region;
    private FlexboxLayout fl_year;
    private FlexboxLayout fl_image_quality;
    private AlertDialog.Builder builder;
    private SmartRefreshLayout srl_tag;

    public TagManageFragment(Context context) {
        this.context = context;
        builder = new AlertDialog.Builder(context);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_tag_manage, container, false);
        initView();
        initData();
        return view;
    }

    private void initView() {
        fl_large = view.findViewById(R.id.fl_large);
        fl_small = view.findViewById(R.id.fl_small);
        fl_region = view.findViewById(R.id.fl_region);
        fl_year = view.findViewById(R.id.fl_year);
        fl_image_quality = view.findViewById(R.id.fl_image_quality);
        srl_tag = view.findViewById(R.id.srl_tag);
        srl_tag.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getAllLarge();
                getAllSmall();
                getAllRegion();
                getAllYear();
                getAllImageQuality();
            }
        });
    }

    private void initData() {
        getAllLarge();
        getAllSmall();
        getAllRegion();
        getAllYear();
        getAllImageQuality();
    }

    private void show(Integer id, String value, String description, FlexboxLayout flexboxLayout,String tag) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.tag_item, flexboxLayout, false);
        TextView tv_id = itemView.findViewById(R.id.tv_id);
        tv_id.setText(String.valueOf(id));
        TextView tv_item = itemView.findViewById(R.id.tv_item);
        tv_item.setText(value);
        ImageView iv_delete = itemView.findViewById(R.id.iv_delete);
        iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String contentStr = tv_item.getText().toString();
                builder.setTitle(null);
                builder.setMessage("真的要删除" + description + "“ " + contentStr + " ”吗？");
                builder.setPositiveButton("取消", null);
                builder.setNegativeButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String id = tv_id.getText().toString();
                        switch (tag){
                            case "大分类":
                                deleteLargeClassification(Integer.parseInt(id), itemView);
                                break;
                            case "小分类":
                                deleteSmallClassification(Integer.parseInt(id), itemView);
                                break;
                            case "地区":
                                deleteRegion(Integer.parseInt(id), itemView);
                                break;
                            case "年份":
                                deleteYear(Integer.parseInt(id), itemView);
                                break;
                            case "画质":
                                deleteImageQuality(Integer.parseInt(id), itemView);
                                break;
                        }
                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();
                dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.RED);
            }
        });
        flexboxLayout.addView(itemView);
    }

    private void add(FlexboxLayout flexboxLayout, String description,String tag) {
        View addBtnView = LayoutInflater.from(context).inflate(R.layout.add_button_item, flexboxLayout, false);
        LinearLayout ll_add_item = addBtnView.findViewById(R.id.ll_add_item);
        srl_tag.finishRefresh();
        ll_add_item.setOnClickListener(new View.OnClickListener() {

            private ColorStateList blueColorStateList = AppCompatResources.getColorStateList(context, R.color.blue);

            @Override
            public void onClick(View v) {
                View editView = View.inflate(context, R.layout.edittext_item, null);
                EditText et_add_content = editView.findViewById(R.id.et_add_content);
                builder.setTitle(description + "添加");
                builder.setMessage(null);
                builder.setPositiveButton("添加", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String contentStr = et_add_content.getText().toString();
                        if (!contentStr.equals("")) {
                            switch (tag){
                                case "大分类":
                                    LargeClassification largeClassification = new LargeClassification();
                                    largeClassification.setClassification(contentStr);
                                    addLargeClassification(largeClassification);
                                    break;
                                case "小分类":
                                    SmallClassification smallClassification = new SmallClassification();
                                    smallClassification.setClassification(contentStr);
                                    addSmallClassification(smallClassification);
                                    break;
                                case "地区":
                                    TheRegion theRegion = new TheRegion();
                                    theRegion.setTheRegion(contentStr);
                                    addRegion(theRegion);
                                    break;
                                case "年份":
                                    TheYear theYear = new TheYear();
                                    theYear.setTheYear(contentStr);
                                    addYear(theYear);
                                    break;
                                case "画质":
                                    ImageQuality imageQuality = new ImageQuality();
                                    imageQuality.setImageQuality(contentStr);
                                    addImageQuality(imageQuality);
                                    break;
                            }
                        }
                    }
                });
                builder.setNegativeButton("取消", null);
                AlertDialog dialog = builder.create();
                dialog.setView(editView);
                dialog.show();
                dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(blueColorStateList);
            }
        });
        flexboxLayout.addView(addBtnView);
    }

    //获取所有大分类
    private void getAllLarge() {
        httpModel.getAllLarge(new DataCallBack() {
            @Override
            public void getData(Object data) {
                List<LargeClassification> datas = Utils.<LargeClassification>objectToList(data);
                HttpClient.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        fl_large.removeAllViews();
                        for (LargeClassification largeClassification : datas) {
                            show(largeClassification.getId(), largeClassification.getClassification(), "类别", fl_large,"大分类");
                        }
                        add(fl_large,"类型","大分类");
                    }
                });
            }
        });
    }

    //添加大分类
    private void addLargeClassification(LargeClassification largeClassification) {
        httpModel.addLargeClassification(largeClassification, new DataCallBack() {
            @Override
            public void getData(Object data) {
                Boolean flag = (Boolean) data;
                HttpClient.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (flag) {
                            getAllLarge();
                            DataApplication.classificationAndImageQualityFlag = false;
                            DataApplication.tagAllocationFlag=false;
                        } else {
                            ToastUtil.show(context, "添加失败");
                        }
                    }
                });
            }
        });
    }

    //删除大分类
    private void deleteLargeClassification(int id, View itemView) {
        httpModel.deleteLargeClassification(id, new DataCallBack() {
            @Override
            public void getData(Object data) {
                Boolean flag = (Boolean) data;
                HttpClient.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (flag) {
                            fl_large.removeView(itemView);
                            DataApplication.classificationAndImageQualityFlag = false;
                            DataApplication.tagAllocationFlag=false;
                        } else {
                            ToastUtil.show(context, "删除失败");
                        }
                    }
                });
            }
        });

    }

    //获取所有小分类
    private void getAllSmall() {
        httpModel.getAllSmall(data -> {
            List<SmallClassification> datas = Utils.<SmallClassification>objectToList(data);
            HttpClient.handler.post(() -> {
                fl_small.removeAllViews();
                for (SmallClassification smallClassification : datas) {
                    show(smallClassification.getId(), smallClassification.getClassification(), "类别", fl_small,"小分类");
                }
                add(fl_small,"类型","小分类");
            });
        });
    }

    //添加小分类
    private void addSmallClassification(SmallClassification smallClassification) {
        httpModel.addSmallClassification(smallClassification, new DataCallBack() {
            @Override
            public void getData(Object data) {
                Boolean flag = (Boolean) data;
                HttpClient.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (flag) {
                            getAllSmall();
                        } else {
                            ToastUtil.show(context, "添加失败");
                        }
                    }
                });
            }
        });
    }

    //删除小分类
    private void deleteSmallClassification(int id, View itemView) {
        httpModel.deleteSmallClassification(id, new DataCallBack() {
            @Override
            public void getData(Object data) {
                Boolean flag = (Boolean) data;
                HttpClient.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (flag) {
                            fl_small.removeView(itemView);
                        } else {
                            ToastUtil.show(context, "删除失败");
                        }
                    }
                });
            }
        });
    }

    //获取所有地区
    private void getAllRegion() {
        httpModel.getAllRegion(data -> {
            List<TheRegion> datas = Utils.<TheRegion>objectToList(data);
            HttpClient.handler.post(() -> {
                fl_region.removeAllViews();
                for (TheRegion theRegion : datas) {
                    show(theRegion.getId(), theRegion.getTheRegion(), "地区", fl_region,"地区");
                }
                add(fl_region,"地区","地区");
            });
        });
    }

    //删除地区
    private void deleteRegion(int id, View itemView) {
        httpModel.deleteRegion(id, new DataCallBack() {
            @Override
            public void getData(Object data) {
                Boolean flag = (Boolean) data;
                HttpClient.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (flag) {
                            fl_region.removeView(itemView);
                        } else {
                            ToastUtil.show(context, "删除失败");
                        }
                    }
                });
            }
        });
    }

    //添加地区
    private void addRegion(TheRegion theRegion) {
        httpModel.addRegion(theRegion, new DataCallBack() {
            @Override
            public void getData(Object data) {
                Boolean flag = (Boolean) data;
                HttpClient.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (flag) {
                            getAllRegion();
                        } else {
                            ToastUtil.show(context, "添加失败");
                        }
                    }
                });
            }
        });
    }

    //获取所有年份
    private void getAllYear() {
        httpModel.getAllYear(data -> {
            List<TheYear> datas = Utils.<TheYear>objectToList(data);
            HttpClient.handler.post(() -> {
                fl_year.removeAllViews();
                for (TheYear theYear : datas) {
                    show(theYear.getId(), theYear.getTheYear(), "年份", fl_year,"年份");
                }
                add(fl_year,"年份","年份");
            });
        });
    }

    //删除年份
    private void deleteYear(int id, View itemView) {
        httpModel.deleteYear(id, new DataCallBack() {
            @Override
            public void getData(Object data) {
                Boolean flag = (Boolean) data;
                HttpClient.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (flag) {
                            fl_year.removeView(itemView);
                        } else {
                            ToastUtil.show(context, "删除失败");
                        }
                    }
                });
            }
        });
    }

    //添加年份
    private void addYear(TheYear theYear) {
        httpModel.addYear(theYear, new DataCallBack() {
            @Override
            public void getData(Object data) {
                Boolean flag = (Boolean) data;
                HttpClient.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (flag) {
                            getAllYear();
                        } else {
                            ToastUtil.show(context, "添加失败");
                        }
                    }
                });
            }
        });
    }

    //获取全部画质信息
    private void getAllImageQuality() {
        httpModel.getAllImageQuality(data -> {
            List<ImageQuality> datas = Utils.<ImageQuality>objectToList(data);
            HttpClient.handler.post(() -> {
                fl_image_quality.removeAllViews();
                for (ImageQuality imageQuality : datas) {
                    show(imageQuality.getId(), imageQuality.getImageQuality(), "画质", fl_image_quality,"画质");
                }
                add(fl_image_quality,"画质","画质");
            });
        });
    }

    //添加画质
    private void addImageQuality(ImageQuality imageQuality) {
        httpModel.addImageQuality(imageQuality, new DataCallBack() {
            @Override
            public void getData(Object data) {
                Boolean flag = (Boolean) data;
                HttpClient.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (flag) {
                            getAllImageQuality();
                            DataApplication.classificationAndImageQualityFlag = false;
                        } else {
                            ToastUtil.show(context, "添加失败");
                        }
                    }
                });
            }
        });
    }

    //删除画质
    private void deleteImageQuality(int id, View itemView) {
        httpModel.deleteImageQuality(id, new DataCallBack() {
            @Override
            public void getData(Object data) {
                Boolean flag = (Boolean) data;
                HttpClient.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (flag) {
                            fl_image_quality.removeView(itemView);
                            DataApplication.classificationAndImageQualityFlag = false;
                        } else {
                            ToastUtil.show(context, "删除失败");
                        }
                    }
                });
            }
        });
    }
}